/**
 * tournote file;
 * company:junqitecch
 * author:xuzuiotao
 * 发表游迹的用户或者
 * mongodb TourNote{
          {
           ID:
           userID:
           title:
           coverURL:
           followedNum:
           commentedNum:
           praisedNum:
           content:[{},{}];
           createTime:
           updateTime:
           latestTime:
           comments:[{}]
           users:[];
          }

 *  
 *
 */



var mongoClient = require('mongodb').MongoClient;
consts = require('../consts');

function getNextTourNote_Value(db, callback){
	console.log('mmmaaa');
	
	   db.collection('ids').findAndModify(
		{'_id': 'autoID' },
		[['_id','asc']],   
		{$inc:{'tournote_value':1,'timeline_value':1}},
		{'new':true,upsert:true},
		callback);

}

function  batchUsers(ids,db,callback){

	if(db===null){
		var url =  consts.MongoURL;//'mongodb://59.110.54.101:2700/tournote';  
		mongoClient.connect(url, consts.MongoOptions,function(err, dbopen) {  
		    if (err)  
		    {  
		    	error = new Error();
		    	error.number= consts.Error.ERROR_GENERAL;
		    	error.message=consts.Message.UNKNOWN;
		    	
		        console.log(err);
		        if(callback){
		        	callback(error,null);
		        }
		    }  
		    else  
		    {  
		    	batchUserInner(ids,dbopen,callback);
		    }  
		});  

	}else{
		batchUserInner(ids,db,callback);
	}

}


function getUser(userID,db,callback){
	var col = db.collection("tournote_user"); 

	
	  col.findOne({'userID':userID},{'userID':1,'iconURL':1,'nickName':1,'_id':0},function(err, user){
		  

	    if(err)
	    {
	        console.log('Error:'+ err);
     	  	error = new Error();

	    	error.number= consts.Error.ERROR_GENERAL;
	    	error.message=consts.Message.UNKNOWN;
	    	
	        if(callback){
	        	callback(error,null);
	        }
 	    	db.close();

	        return;
	    }
	    
	    	db.close();

	    if(callback){
          	  callback(null,user);
	    }
	  });
	
}



function batchUserInner(ids,db,callback){
	var col = db.collection("tournote_user"); 
	
	  console.log(ids);
	  col.find({'userID':{$in:ids}},{'userID':1,'iconURL':1,'nickName':1,'_id':0}).toArray(function(err, result){
	    if(err)
	    {
	        console.log('Error:'+ err);
     	  	error = new Error();

	    	error.number= consts.Error.ERROR_GENERAL;
	    	error.message=consts.Message.UNKNOWN;
	    	
	    	db.close();
	        if(callback){
	        	callback(error,null);
	        }
	        return;
	    }
	    
    	db.close();

	    if(callback){
        	  callback(null,result);
		}
	  });
	
}


/*
 * 添加游迹用户，
 */

function updateExist(user, callback){

	
		var url =  consts.MongoURL;//'mongodb://59.110.54.101:2700/tournote';  
		mongoClient.connect(url,consts.MongoOptions, function(err, dbopen) {  
		    if (err)  
		    {  
		    	error = new Error();
		    	error.number= consts.Error.ERROR_GENERAL;
		    	error.message=consts.Message.UNKNOWN;
		    	
		        console.log(err);
		        if(callback){
		        	callback(error,null);
		        }
		    }  
		    else  
		    {
		     	var col = dbopen.collection("tournote_user");

		    	
	    		var tuser= {};
	    		tuser._id=user.userID;
	    		tuser.userID=user.userID;
	    		tuser.iconURL=user.iconURL;
	    		tuser.nickName = user.nickName;
    	     	
		     	 col.update({'_id':user.userID},{$set:tuser}, {'upsert':false},function(err, result){
		     		if(err)
		     		    {
		     		        console.log('Error:'+ err);
		     	     	  	error = new Error();

		     		    	error.number= consts.Error.ERROR_GENERAL;
		     		    	error.message=consts.Message.UNKNOWN;
			     	    	dbopen.close();

		     		        if(callback){
		     		        	callback(error,null);
		     		        }
		     		        return;
		     		    }
		     	    	dbopen.close();

		     		    if(callback){
		     	        	  callback(null,null);
		     			
		     			}
		    
		     	 });
		  
		    }
		
    	});

		
}


/*
 * 添加游迹用户，
 */

function insert(user,db,callback){

	if(db===null){
		var url =  consts.MongoURL;//'mongodb://59.110.54.101:2700/tournote';  
		mongoClient.connect(url,consts.MongoOptions, function(err, dbopen) {  
		    if (err)  
		    {  
		    	error = new Error();
		    	error.number= consts.Error.ERROR_GENERAL;
		    	error.message=consts.Message.UNKNOWN;
		    	
		        console.log(err);
		        if(callback){
		        	callback(error,null);
		        }
		    }  
		    else  
		    {  
		    	upsertUserInner(user,dbopen,callback);
		    }  
		});  

	}else{
    	upsertUserInner(user,db,callback);
	}

}

/*
 * 内部函数添加或者修改游迹用户
 */
function upsertUserInner(user,db,callback){
	var col = db.collection("tournote_user"); 

	 col.update({'_id':user.userID},{$set:user}, {'upsert':true},function(err, result){
	if(err)
	    {
	        console.log('Error:'+ err);
     	  	error = new Error();

	    	error.number= consts.Error.ERROR_GENERAL;
	    	error.message=consts.Message.UNKNOWN;
 	    	db.close();

	        if(callback){
	        	callback(error,null);
	        }
	        return;
	    }
	   
 	   db.close();

	    if(callback){
        	  callback(null,null);
		
		}
	  });
	
}

module.exports = {
		insert:insert,
		batchUsers:batchUsers,
		updateExist:updateExist,
		getUser:getUser,

	};


